package com.kgd.news.controller.admin;

import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.redis.Cache;
import com.kgd.news.bean.VideoInfoModel;
import com.kgd.news.entity.SysDict;
import com.kgd.news.entity.Xcxmessage;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.CacheUtils;
import com.kgd.news.utils.StringUtils;
import com.kgd.news.utils.wx.WxHelper;

import java.util.List;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/6/20
 * @Time: 下午4:18
 */
public class MessageController extends BaseController {
    @Override
    public void list() {
    }

    @Override
    public void edit() {
        String title = CacheUtils.getContextCache("messagetitle");
        String category = CacheUtils.getContextCache("messagecategory");
        String updatetime = CacheUtils.getContextCache("messageupdatetime");
        String feature = CacheUtils.getContextCache("messagefeature");
        String page = CacheUtils.getContextCache("messagepage");
        String cout = CacheUtils.getContextCache("messagepresult");
        long cutTime = System.currentTimeMillis() - 7 * 24 * 60 * 1000 * 60L;
        List<Xcxmessage> xcxmessageList = Xcxmessage.dao.find("select * from xcxmessage where createtime > " + cutTime + " group by openid");
        int validcount = xcxmessageList.size();
        setAttr("title", title);
        setAttr("category", category);
        setAttr("updatetime", updatetime);
        setAttr("feature", feature);
        setAttr("page", page);
        setAttr("validcount", validcount);
        if (!StringUtils.isEmpty(category)) {
            setAttr("result", "共发送消息模板" + cout + "次");
        }
        render("message_edit.html");
    }

    public void send() {
        getFile("img");
        String title = getPara("title");
        String category = getPara("category");
        String updatetime = getPara("updatetime");
        String feature = getPara("feature");
        String page = getPara("page");
        CacheUtils.setContextCache("messagetitle", title);
        CacheUtils.setContextCache("messagecategory", category);
        CacheUtils.setContextCache("messageupdatetime", updatetime);
        CacheUtils.setContextCache("messagefeature", feature);
        CacheUtils.setContextCache("messagepage", page);

        VideoInfoModel videoInfoModel = new VideoInfoModel();
        videoInfoModel.setKeyword1(title);
        videoInfoModel.setKeyword2(category);
        videoInfoModel.setKeyword3(updatetime);
        videoInfoModel.setKeyword4(feature);
        int cout = WxHelper.sendTemplate(videoInfoModel, page, "");
        CacheUtils.setContextCache("messagepresult", cout + "");
        setAttr("result", "共发送消息模板" + cout + "次");
        render("message_result.html");
    }

    @Override
    public void save() {
    }

    @Override
    public void del() {
    }

}
